package internal

import (
	"encoding/json"
	"fmt"
	"os"
	"path"

	"gitee.com/night-tc/gobige/common"
)

/*
名字管理器

给机器人用的，加载配置也就一次，不考虑别的逻辑
*/
type NameMgr struct {
	Family []string
	Name   []string
}

var (
	nameMgr *NameMgr
)

func LoadName() {
	nameMgr = new(NameMgr)
	{
		familyp := path.Join(common.GetResPath(), "config", "FamilyName.json")
		data, err := os.ReadFile(familyp)
		if err != nil {
			panic(err)
		}
		nameMgr.Family = make([]string, 0)
		err = json.Unmarshal(data, &nameMgr.Family)
		if err != nil {
			panic(err)
		}
	}
	{
		namep := path.Join(common.GetResPath(), "config", "FirstName.json")
		data, err := os.ReadFile(namep)
		if err != nil {
			panic(err)
		}
		nameMgr.Name = make([]string, 0)
		err = json.Unmarshal(data, &nameMgr.Name)
		if err != nil {
			panic(err)
		}
	}
}

func GetName() (result string) {
	key, _ := common.GetGlobalRand().GetRandomIndex2(len(nameMgr.Family))
	key2, _ := common.GetGlobalRand().GetRandomIndex2(len(nameMgr.Name))
	result = fmt.Sprintf("%s%s", nameMgr.Family[key], nameMgr.Name[key2])
	return
}
